'use strict';

const fs = require('fs');
const XLSX = require('node-xlsx');
const path = require('path');
const fileNameType = ['.xlsx', 'csv'];

/**
 * 数组的第一项为第一个sheet
 * name:"sheet1"
 * data: 第一个数组元素为第一行内容(表头),其余行为内容
 * @param {*} excelFile
 */
exports.readExcel = excelFile => {
    const filePath = excelFile.filepath;
    try {
        if (!fileNameType.includes(path.extname(filePath))) {
            return [];
        }
        const workbook = XLSX.parse(fs.readFileSync(filePath));
        return workbook;
    } catch (error) {
        throw error;
    } finally {
        if (fs.existsSync(filePath)) {
            fs.unlinkSync(filePath);
        }
    }
};
